/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.PurchaseOrder;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.Stock;
import static com.reddys.rad.inventory.service.PurchaseReturnService.em;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import com.reddys.rad.inventory.utils.Utilities;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author REDDYS
 */
public class PurchaseService {

    public ObservableList getPurchaseOrderList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("PurchaseOrder.findAll");
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            PurchaseOrder purchaseOrder = (PurchaseOrder) iter.next();
            data.add(purchaseOrder);
        }
        return data;
    }

    public ObservableList<PurchaseOrderDetails> getPurchaseOrderDetailsList(String purchaseOrderID) {
                 ObservableList<PurchaseOrderDetails> data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("PurchaseOrderDetails.findByPurchaseOrderId");
        query.setParameter("purchaseOrderId", purchaseOrderID);
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            PurchaseOrderDetails purchaseOrderDetails = (PurchaseOrderDetails) iter.next();
            Query queryProduct = em.createNamedQuery("Product.findByProductId");
            queryProduct.setParameter("productId", purchaseOrderDetails.getProductId());
            List productIDList = queryProduct.getResultList();
            if(productIDList.size()>0){
                Product product  = (Product) productIDList.get(0);
                Query queryProductCategory = em.createNamedQuery("ProductCategory.findByProductCategoryId");
                queryProductCategory.setParameter("productCategoryId", product.getProductCategoryId());
                List productCategoryList = queryProductCategory.getResultList();
                if(productCategoryList.size()>0){
                    ProductCategory productCategory = (ProductCategory) productCategoryList.get(0);
                    purchaseOrderDetails.setProductCategory(productCategory.getProductCategoryDescription());
                }
            }
            data.add(purchaseOrderDetails);
        }
        return data;

    }
     public static Stock getStock(String productId) {
         ArrayList<Stock> stockList = new ArrayList<Stock>();
         Stock stock = null;
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Stock.findByProductId");
        query.setParameter("productId",productId);
        List list = query.getResultList();
        Iterator iter = list.iterator();
        while(iter.hasNext()){
            stock = (Stock) iter.next();
           // stockList.add(stock);
        }
        return stock;
    }
    
    public void updateStock(Stock stock,EntityManager em) {
       if(Utilities.isNull(stock.getStockId())){
      em.persist(stock);
       }else{
            em.merge(stock);
       }

    }
    
}
